package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.community.entity.Notice;
import com.wanmait.community.mapper.NoticeMapper;
import com.wanmait.community.service.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService{
@Resource
private NoticeMapper noticeMapper;
    @Override
    public PageInfo<Notice> selectNoticeList(Notice notice, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Date endCreatTime = null;
        // 处理 creatTime
        if (notice.getCreatTime() != null) {
            endCreatTime = Date.from(
                    notice.getCreatTime().toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime()
                            .withHour(23).withMinute(59).withSecond(59)
                            .atZone(ZoneId.systemDefault())
                            .toInstant()
            );
        }
        List<Notice> list = noticeMapper.selectNoticeList(notice,endCreatTime);
        PageInfo<Notice> pageInfo = new PageInfo<>(list,5);
        return pageInfo;
    }

    @Override
    public void insertNotice(Notice notice) {
        noticeMapper.insertNotice(notice);
    }

    @Override
    public Notice selectNoticeById(Integer id) {
        return noticeMapper.selectNoticeById(id);

    }

    @Override
    public void updateNotice(Integer id) {
        noticeMapper.deleteNoticeById(id);
    }

    @Override
    public Notice selectNoticeByNoticeId(Integer id) {
        return noticeMapper.selectNoticeByNoticeId(id);
    }

    @Override
    public void updateNoticeby(Notice notice) {
        noticeMapper.updateNoticeby(notice);
    }
}
